package crm.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLIntegrityConstraintViolationException;

import javax.swing.JOptionPane;

import crm.daoInterfaces.PriorityDao;
import crm.dto.Priority;
import crm.implementor.PriorityImp;
import crm.utility.Browser;
import crm.view.masterMagment.MasterManagment;
import crm.viewframework.Validater;


public class PriorityManagmentController extends Browser implements ActionListener
{
	private MasterManagment view;
	private Priority model;
	private Priority[] priorities;
	
	
	
	public PriorityManagmentController()
	{
		
	}


	public PriorityManagmentController(MasterManagment view, Priority model) {
		super();
		this.view = view;
		this.model = model;
		view.getLblName().setText("Priority Name");
		view.actionListener(this);
		PriorityDao pr=new PriorityImp();
		try {
			priorities=pr.retrieveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		maxLength=priorities.length;
		index=-1;
		
	}


	public MasterManagment getView() {
		return view;
	}


	public void setView(MasterManagment view) {
		this.view = view;
	}


	public Priority getModel() {
		return model;
	}


	public void setModel(Priority model) {
		this.model = model;
	}
	
	
	
	public void reactOnSave()
	{
		String name=view.getTfName().getText();
		PriorityDao priority=new PriorityImp();
		model=new Priority(name,1);
		
		try 
		{
			priority.add(model);
			JOptionPane.showMessageDialog(null, "Data successfully added");
		} 
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
			e.printStackTrace();
		}
		catch (Exception e) 
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
			e.printStackTrace();
		}
	}
	
	public void reactOnMarkInactive() throws Exception
	{
		String name=view.getTfName().getText();
		PriorityDao priority=new PriorityImp();
		Priority p=null;
		try 
		{
			p=priority.retrieve("priorityName", name);
			if(p.getStatus()==1)
				p.setStatus(0);
			else
				p.setStatus(1);
		}
		catch (Exception e) 
		{
			
			e.printStackTrace();
		}
		priority.update(p);
		
	}
	
	public void reactOnUpdate()
	{
		
	}
	
	public void reactOnSearch()
	{
		String name=view.getTfName().getText();
		PriorityDao priority=new PriorityImp();
		model=null;
		try 
		{
			model=priority.retrieve("priorityName", name);
			if(model==null)
			 {
				 JOptionPane.showMessageDialog(null," DATA NOT FOUND !");
				 refresh();
			 }
		} 
		catch (Exception e) 
		{
			
			e.printStackTrace();
		}
	}
	
	
	
	public void fillViewFormFromModel() 
	{
		model=this.priorities[index];
		view.getTfName().setText(model.getPriorityName());
		if(model.getStatus()==1)
		{
			view.getBtnStatus().setText("MARK INACTIVE");
			view.getLblStatus().setText("ACTIVE");
		}
		
		if(model.getStatus()==0)
		{
			view.getBtnStatus().setText("MARK ACTIVE");
			view.getLblStatus().setText("INACTIVE");
			
		}
		
	}

	

	private void refresh()
	{
		
		Validater validater = new Validater(view);
		validater.refresh();
	}


	@Override
	public void actionPerformed(ActionEvent arg)
	{
		String k=arg.getActionCommand();
		if(k.contains("SAVE"))
		{
			reactOnSave();
			JOptionPane.showMessageDialog(null,"SUCCSESS FULLY SAVED !");
			refresh();
		}
		if(k.contains("status"))
		{
			try {
				reactOnMarkInactive();
				JOptionPane.showMessageDialog(null,"SUCCSESS FULLY UPDATED !");
				refresh();
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		if(k.contains("<<"))
		{
			try {
				
				reactOnBack();
			} catch (Exception e) {
			
				e.printStackTrace();
			}
		}
		if(k.contains("SEARCH"))
		{
			try {
				reactOnSearch();
				
			} catch (Exception e) {
			
				e.printStackTrace();
			}
		}
		if(k.contains(">>"))
		{
			try {
				reactOnForward();
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		if(k.contains("CLEAR"))
		{
			
		}
		
		
	}
	
	
	

}
